package io.renren.wh.merchant.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.wh.merchant.entity.MerchantKeyEntity;
import io.renren.wh.merchant.service.MerchantKeyService;
import io.renren.wh.merchant.vo.MerchantChannelRateVo;
import io.renren.wh.merchant.vo.MerchantOrderVo;
import io.renren.wh.order.entity.OrderInfoEntity;
import io.renren.wh.order.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-08-05 10:35
 **/

@Api(value = "商户个人信息接口",tags = "商户个人信息接口")
@RestController
@RequestMapping(value = "wh/merchant")
public class MerchantInfoController extends AbstractController {

    @Autowired
    private MerchantKeyService merchantKeyService;

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 查询商户基本信息获取APPID和秘钥
     * @return
     */
    @ApiOperation(value = "查询商户基本信息",notes = "查询商户基本信息",httpMethod = "GET")
    @RequestMapping(value = "/getMerchantInfo")
    public R getMerchantInfo() {
        //获取当前登录用户信息
        SysUserEntity user = getUser();
        String userName = user.getUsername();
        MerchantKeyEntity merchant = merchantKeyService.selectByUserName(userName);

        if (merchant == null) {
            return R.error("请先登录");
        }

        MerchantKeyEntity merchantKeyEntity = merchantKeyService.queryByMerchantId(merchant.getMerchantid());

        return R.ok().put("merchantKeyInfo",merchantKeyEntity);
    }

    /**
     * 查询当前商户的通道及费率
     * @return
     */
    @ApiOperation(value = "查询当前商户的通道及费率",notes = "查询当前商户的通道及费率",httpMethod = "GET")
    @GetMapping(value = "/getChannelRateInfo")
    public R getChannelRateInfo() {

        //获取当前登录用户信息
        SysUserEntity user = getUser();
        String userName = user.getUsername();
        MerchantKeyEntity merchant = merchantKeyService.selectByUserName(userName);

        if (merchant == null) {
            return R.ok();
        }

        List<MerchantChannelRateVo> channelRateVo = merchantKeyService.getChannelRateInfo(merchant.getMerchantid());

        return R.ok().put("channelRateVo",channelRateVo);
    }


    /**
     * 查询当前商户的订单信息
     * @return
     */
    @ApiOperation(value = "查询当前商户的订单信息",notes = "查询当前商户的通道及费率",httpMethod = "GET")
    @GetMapping(value = "/getMerchantOrderInfo")
    public R getMerchantOrderInfo(
                                    @ApiParam(name = "page", value = "页码", required = false)
                                      @RequestParam(required = false) String page,
                                  @ApiParam(name = "limit", value = "每页条数", required = false)
                                      @RequestParam(required = false) String limit,
                                  @ApiParam(name = "merchantname", value = "商户名称", required = false)
                                      @RequestParam(required = false) String merchantname,
                                  @ApiParam(name = "channelname", value = "通道名称", required = false)
                                      @RequestParam(required = false) String channelname,
                                  @ApiParam(name = "merchantordersn", value = "商户订单号", required = false)
                                      @RequestParam(required = false) String merchantordersn,
                                  @ApiParam(name = "systemordersn", value = "系统订单号", required = false)
                                      @RequestParam(required = false) String systemordersn,
                                  @ApiParam(name = "starttime", value = "开始日期", required = false)
                                      @RequestParam(required = false) String starttime,
                                  @ApiParam(name = "endtime", value = "结束日期", required = false)
                                      @RequestParam(required = false) String endtime,
                                  @ApiParam(name = "interfaceid", value = "支付方式(接口ID)", required = false)
                                      @RequestParam(required = false) Integer interfaceid,
                                  @ApiParam(name = "orderstatus", value = "订单状态：0---待充值   1---已充值   2---已失效", required = false)
                                      @RequestParam(required = false) Integer orderstatus,
                                    @ApiParam(name = "callbackStatus", value = "回调状态：0---未回调   1---已回调", required = false)
                                    @RequestParam(required = false) Integer callbackStatus
    )
    {

        //获取当前登录用户信息
        SysUserEntity user = getUser();
        String userName = user.getUsername();
        MerchantKeyEntity merchant = merchantKeyService.selectByUserName(userName);

        if (merchant == null) {
            return R.ok();
        }

        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",limit);
        params.put("merchantname",merchantname);
        params.put("channelname",channelname);
        params.put("merchantordersn",merchantordersn);
        params.put("systemordersn",systemordersn);
        params.put("starttime",starttime);
        params.put("endtime",endtime);
        params.put("interfaceid",interfaceid);
        params.put("orderstatus",orderstatus);
        params.put("callbackStatus",callbackStatus);
        params.put("merchantId",merchant.getMerchantid());


        PageUtils pageUtils = orderInfoService.queryPage(params);

        if (merchant.getMerchantid() != null && pageUtils.getTotalCount() > 0) {
            List<MerchantOrderVo> merchantOrderVos = new ArrayList<>();
            for (Object o : pageUtils.getList()) {
                OrderInfoEntity orderInfo = (OrderInfoEntity) o;
                MerchantOrderVo merchantOrderVo = new MerchantOrderVo();
                BeanUtils.copyProperties(orderInfo,merchantOrderVo);
                merchantOrderVos.add(merchantOrderVo);
            }
            pageUtils.setList(merchantOrderVos);
        }

        return R.ok().put("page", pageUtils);
    }
}
